package com.lml.service;
import com.lml.dao.IUserDao;
import com.lml.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Objects;

@Service
public class UserServiceImpl implements UserService {

    private final IUserDao userDao;
    @Autowired
    public UserServiceImpl(IUserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public int register(User user) {
        if (user == null || Objects.isNull(user.getUsername()) || user.getUsername().isBlank()) {
            return 400;
        }
        User checkUsername = userDao.checkUsername(user.getUsername());
        if (checkUsername != null) {
            return 212;
        }
        try {
            int result = userDao.add(user);
            if (result != 1) {
                return 213;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return 500;
        }

        return 200;
    }
}
